<?php
//------ restaurants ---------------------------// 
class TipoCocina extends Generic {
	private static $instance;
	public function getInstance() {
		if($instance === null) {
			$instance = new TipoCocina();
		}
		return $instance;
	}
	
	function __construct() {
		parent::Generic('tipococina');
		$this->addProperties(array(
				'orderId' => 'NUM', 'ico' => 'STR')
		);
	}
	
	function gRestaurantCocina() {
		if ($this ->id) {
			$tmp = ResTipoCocina::getInstance();
			$tmp ->addWhere("tipococina.tipococinaId='{$this ->id}' AND restaurant.estado='". LBN_ADMIN_STATUS_ON ."'");
			$tmp ->loadList();
			return $tmp ->rMore() ? $tmp : false;
		}
	}
	
}

class TipoHorario extends Generic {
	private static $instance;
	public function getInstance() {
		if($instance === null) {
			$instance = new TipoHorario();
		}
		return $instance;
	}

	function __construct() {
		parent::Generic('tipohorario');
		$this->addProperties(array(
			'orderId' => 'NUM'));
		$this ->_maxAdded = 3;
	}
}

class MedioPago extends Generic {
	private static $instance;
	public function getInstance() {
		if($instance === null) {
			$instance = new MedioPago();
		}
		return $instance;
	}

	function __construct() {
		parent::Generic('mediopago');
		$this->addProperties(array(
				'orderId' => 'NUM' ));
		
		
	}
}

class Ciudad extends Generic {
	private static $instance;
	public function getInstance() {
		if($instance === null) {
			$instance = new Ciudad();
		}
		return $instance;
	}

	function __construct() {
		parent::Generic('ciudad');
	}
	
	function gZonas() {
		if ($this ->id) {
			$tmp = Zona::getInstance();
			$tmp ->addWhere("ciudad.ciudadId='{$this ->id}'");
			$tmp ->loadList();
			return $tmp ->rMore() ? $tmp : false;
		}
	}
	
	function gZonasCount() {
		$ret = 0;
		if ($tmp = $this ->gZonas()) 
			$ret = $tmp ->rCount();
		return $ret;
	} 
	
}

class Zona extends Generic {
	private static $instance;
	public function getInstance() {
		if($instance === null) {
			$instance = new Zona();
		}
		return $instance;
	}

	function __construct() {
		parent::Generic('zona');
		$this->addProperties(array(
			'ciudad' => 'OBJ'
		));
	}
}



class Restaurant extends Generic {
	public $_horario, $_tipococina, $_mediopago, $_zonaentrega = array();
	private static $instance;
	public function getInstance() {
		if($instance === null) {
			$instance = new Restaurant();
		}
		return $instance;
	}
	function __construct() {
		parent::Generic('restaurant');
		$this->addProperties(array(
			'subtitulo' 	 => 'STR',
			'ordenMin'  	 => 'DEC',
			'costoEntrega'   => 'DEC',
			'tiempoEntrega'  => 'STR',
			'seo'  			 => 'STR',
			'zonDescripcion' => 'STR',
			'zonLat'		 => 'STR',
			'zonLong' 		 => 'STR'
		));
	}
	
	private function _updateRelationship() {
		if ($this ->id) {
			$this ->query("DELETE FROM {$this ->gTable('horario')} WHERE restaurantId='{$this ->id}'");
			$this ->query("DELETE FROM {$this ->gTable('res_tipococina')} WHERE restaurantId='{$this ->id}'");
			$this ->query("DELETE FROM {$this ->gTable('res_mediopago')} WHERE restaurantId='{$this ->id}'");
			$this ->query("DELETE FROM {$this ->gTable('zonaentrega')} WHERE restaurantId='{$this ->id}'");

			if (sizeof($this ->_horario) > 0) {
				foreach ($this ->_horario as $k => $v) {
					foreach ($v as $d => $h) {
						$this ->query("INSERT INTO {$this ->gTable('horario')} (restaurantId, tipohorarioId, dia, horaDesde, horaHasta) VALUES ('{$this ->id}', '$k', '$d', '{$h["from"]}', '{$h["to"]}')");
					}
				}
			}
			
			if (sizeof($this ->_tipococina) > 0) 
				foreach ($this ->_tipococina as $item)
					$this ->query("INSERT INTO {$this ->gTable('res_tipococina')} (restaurantId, tipococinaId) VALUES ('{$this ->id}', '$item')");
			if (sizeof($this ->_mediopago) > 0)
				foreach ($this ->_mediopago as $item)
					$this ->query("INSERT INTO {$this ->gTable('res_mediopago')} (restaurantId, mediopagoId) VALUES ('{$this ->id}', '$item')");
			if (sizeof($this ->_zonaentrega) > 0)
				foreach ($this ->_zonaentrega as $item)
				$this ->query("INSERT INTO {$this ->gTable('zonaentrega')} (restaurantId, zonaId) VALUES ('{$this ->id}', '$item')");
			
		}
	}
	
	
	function add() {
		if (parent::add ()) 
			$this ->_updateRelationship();
	}
	
	function update() {
		if (parent::update())
			$this ->_updateRelationship();
	}
	
	function pSelectCheckBoxTipoCocina () {
		$items = TipoCocina::getInstance();
		if (!$items ->loadItems()) {
			print '<b>No existe a&uacute;n, tipos de cocinas, <a href="tipococina-add.php">crear.</a></b>';
		} else {
			print '<select name="tipococinaId" id="tipococinaId" multiple="multiple" style="width:520px;"  >';
			print '	<option value=""></option>';
			$selecteds = $this ->gTipoCocina();
			while ($option = $items ->rNext()) {
				$yesNo = method_exists($selecteds, '_searchTipoCocina') ? ($selecteds ->_searchTipoCocina($option ->id) !== false ? "selected" : "" ) : "";
				print "<option value='{$option ->id}' $yesNo >{$option ->titulo}</option>";
			}
			print '</select>';
		}
	}  
	
	function gTipoCocina() {
		if ($this ->id) {
			$tmp = ResTipoCocina::getInstance();
			$tmp ->addWhere("restaurant.restaurantId='{$this ->id}' AND tipococina.estado='". LBN_ADMIN_STATUS_ON ."'");
			$tmp ->loadList();
			return $tmp ->rMore() ? $tmp : false;
		}
	}	
	function gTipoCocinaCount() {
		$ret = 0;
		if ($tmp = $this ->gTipoCocina())
			$ret = $tmp ->rCount();
		return $ret;
	}
	
	function pSelectCheckBoxMedioPago () {
		$items = MedioPago::getInstance();
		if (!$items ->loadItems()) {
			print '<b>No existe a&uacute;n, medios de pago, <a href="mediopago-add.php">crear.</a></b>';
		} else {
			print '<select name="mediopagoId" id="mediopagoId" multiple="multiple" style="width:520px;"  >';
			print '	<option value=""></option>';
			$selecteds = $this ->gMedioPago();
			while ($option = $items ->rNext()) {
				$yesNo = method_exists($selecteds, '_searchMedioPago') ? ($selecteds ->_searchMedioPago($option ->id) !== false ? "selected" : "" ) : "";
				print "<option value='{$option ->id}' $yesNo >{$option ->titulo}</option>";
			}
			print '</select>';
		}
	}
	
	
	function gMedioPago() {
		if ($this ->id) {
			$tmp = ResMedioPago::getInstance();
			$tmp ->addWhere("restaurant.restaurantId='{$this ->id}' AND mediopago.estado='". LBN_ADMIN_STATUS_ON ."'");
			$tmp ->loadList();
			return $tmp ->rMore() ? $tmp : false;
		}
	}
	
	
	function gMedioPagoCount() {
		$ret = 0;
		if ($tmp = $this ->gMedioPago())
			$ret = $tmp ->rCount();
			return $ret;
	}
	
	
	/* deprecated.  */
// 	function gMenu() {
// 		if ($this ->id) {
// 			$tmp = ResMenu::getInstance();
// 			$tmp ->addWhere("restaurant.restaurantId='{$this ->id}' AND menu.estado='". LBN_ADMIN_STATUS_ON ."'");
// 			$tmp ->loadList();
// 			return $tmp ->rMore() ? $tmp : false;
// 		}
// 	}
	
// 	function gMenuCount() {
// 		$ret = 0;
// 		if ($tmp = $this ->gMenu())
// 			$ret = $tmp ->rCount();
// 		return $ret;
// 	}
	/* end deprecated */
	
	function gCategoria() {
		if ($this ->id) {
			$tmp = Categoria::getInstance();
			$tmp ->addWhere("restaurant.restaurantId='{$this ->id}' AND ". $tmp ->gTable().".estado='". LBN_ADMIN_STATUS_ON ."'");
			$tmp ->loadList();
			return $tmp ->rMore() ? $tmp : false;
		}
	}
	
	function gCategoriaCount() {
		$ret = 0;
		if ($tmp = $this ->gCategoria())
			$ret = $tmp ->rCount();
		return $ret;
	}
	
	
	function gZona() {
		if ($this ->id) {
			$tmp = ResZona::getInstance();
			$tmp ->addWhere("restaurant.restaurantId='{$this ->id}' AND zona.estado='". LBN_ADMIN_STATUS_ON ."'");
			$tmp ->loadList();
			return $tmp ->rMore() ? $tmp : false;
		}
	}
	
	function gZonaCount() {
		$ret = 0;
		if ($tmp = $this ->gZona())
			$ret = $tmp ->rCount();
		return $ret;
	}
	
	function gDayHorary($type, $day)  {
		$return = false;
		if ($this ->id) {
			$h = new ResHorario();
			if($h ->loadByFilter("dia='{$day}' AND " .$h ->gTable(). ".tipohorarioId='{$type}' AND " .$h ->gTable(). ".restaurantId='{$this ->id}'")) {
				$return = $h;	
			}
		}
		return $return; 
	}
	
	
	function pHorary() {
		$return = "";
		global $weekDays, $timePart, $timeHour, $timeMin;
		$heads = new TipoHorario();
		if ($heads ->loadItems("#orderId ASC", "#estado='". LBN_ADMIN_STATUS_ON . "'")) {
				while ($head = $heads ->rNext()) { ?>
	
	<table class="horary"  border="0" cellspacing="0" cellpadding="0"><caption><?= $head ->titulo ?></caption>
		<tr>
	    <th width="15%" class="" >D&Iacute;a</th>
	    <th width="85%" ><table  border="0" cellspacing="0" cellpadding="0">
	      <tr  >
	        <td class="from" >Desde</td>
	        <td class="to">Hasta</td>
	      </tr>
	    </table></th>
	  </tr>
	<?php
					foreach ($weekDays as $k => $val): 
	?>
	   <tr >
	    <td width="15%" class="day"  ><?= $val ?></td>
	    <td width="85%" ><table  border="0" cellspacing="0" cellpadding="0">
	      <tr>
	        <td  class="from sep-right"><table  border="0" cellspacing="0" cellpadding="0">
	          <tr>
	            <td><input name="<?= 'day['. $head ->id  .']['. $k .'][from]' ?>" type="text" class="hour" value="<?= ($hour = $this ->gDayHorary($head ->id, $k)) ? $hour ->horaDesde : ""  ?>" /></td>
	          </tr>
	        </table></td>
	        <td class="to" ><table  border="0" cellspacing="0" cellpadding="0">
	          <tr>
	            <td><input name="<?= 'day['. $head ->id  .']['. $k .'][to]' ?>" type="text" class="hour" value="<?=  ($hour = $this ->gDayHorary($head ->id, $k)) ? $hour ->horaHasta : ""   ?>" /></td>
	          </tr>
	        </table></td>
	      </tr>
	    </table></td>
	  </tr>  
	<?php 			endforeach;?>
	</table>			
	<?php 
				}
				
			}
		}
	
	
	/*function getHorary() {
		$return = "";
		global $weekDays, $timePart, $timeHour, $timeMin;
		$heads = new TipoHorario();
		if ($heads ->loadItems("#orderId ASC", "#estado='". LBN_ADMIN_STATUS_ON . "'")) {
			while ($head = $heads ->rNext()) { ?>

<table class="horary"  border="0" cellspacing="0" cellpadding="0"><caption><?= $head ->titulo ?></caption>
	<tr>
    <th width="15%" class="" >D&Iacute;a</th>
    <th width="85%" ><table  border="0" cellspacing="0" cellpadding="0">
      <tr  >
        <td class="from" >Desde</td>
        <td class="to">Hasta</td>
      </tr>
    </table></th>
  </tr>
<?php
				foreach ($weekDays as $k => $val): 
?>
   <tr >
    <td width="15%" class="day"  ><?= $val ?></td>
    <td width="85%" ><table  border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td  class="from sep-right"><table  border="0" cellspacing="0" cellpadding="0">
          <tr> 
            <td><?php $this ->arrayToSelect(arrayNumeric2Char($timeHour), 'day[lunes][from][hour]', '', '--')   ?> </td>
            <td><?php $this ->arrayToSelect(arrayNumeric2Char($timeMin), 'day[lunes][from][min]', '', '--')   ?></td>
            <td><?php $this ->arrayToSelect($timePart, 'day[lunes][from][part]', '', '--')   ?></td>
          </tr>
        </table></td>
        <td class="to" ><table  border="0" cellspacing="0" cellpadding="0">
          <tr> 
            <td><?php $this ->arrayToSelect(arrayNumeric2Char($timeHour), 'day[lunes][to][hour]', '', '--')   ?> </td>
            <td><?php $this ->arrayToSelect(arrayNumeric2Char($timeMin), 'day[lunes][to][min]', '', '--')   ?></td>
            <td><?php $this ->arrayToSelect($timePart, 'day[lunes][to][part]', '', '--')   ?></td>
          </tr>
        </table></td>
      </tr>
    </table></td>
  </tr>  
<?php 			endforeach;?>
</table>			
<?php 
			}
			
		}
	}*/
	
}

class ResHorario extends TznDb {
	private static $instance;
	public function getInstance() {
		if($instance === null) {
			$instance = new ResHorario();
		}
		return $instance;
	}

	function __construct() {
		parent::TznDb('horario');
		$this->addProperties(array(
				'id' 		 	 => 'UID',
				'restaurant' 	 => 'OBJ',
				'tipohorario'	 => 'OBJ',
				'dia' 			 => 'STR',
				'horaDesde' 	 => 'STR',
				'horaHasta' 	 => 'STR'
		));
	}
	
	
}








class ResZona extends TznDb {
	private static $instance;
	public function getInstance() {
		if($instance === null) {
			$instance = new ResZona();
		}
		return $instance;
	}

	function __construct() {
		parent::TznDb('zonaentrega');
		$this->addProperties(array(
			'id' 		 => 'UID',
			'restaurant' => 'OBJ',
			'zona' => 'OBJ'
		));
	}
}


class ResTipoCocina extends TznDb {
	private static $instance;
	public function getInstance() {
		if($instance === null) {
			$instance = new ResTipoCocina();
		}
		return $instance;
	}
	
	function __construct() {
		parent::TznDb('res_tipococina');
		$this->addProperties(array(
			'id' 		 => 'UID',
			'restaurant' => 'OBJ',
			'tipococina' => 'OBJ'
		));
	}
	
	function _searchTipoCocina($id) {
		$return = false;
		$this->rReset();
		while($objItem = $this->rNext()) {
			if ($objItem->tipococina ->id == $id ) {
				$return = $objItem;
				break;
			}
		}
		return $return;
	}
	
}



class ResMedioPago extends TznDb {
	private static $instance;
	public function getInstance() {
		if($instance === null) {
			$instance = new ResMedioPago();
		}
		return $instance;
	}

	function __construct() {
		parent::TznDb('res_mediopago');
		$this->addProperties(array(
			'id' 		 => 'UID',
			'restaurant' => 'OBJ',
			'mediopago'  => 'OBJ'
		));
	}
	
	function _searchMedioPago($id) {
		$return = false;
		$this->rReset();
		while($objItem = $this->rNext()) {
			if ($objItem->mediopago ->id == $id ) {
				$return = $objItem;
				break;
			}
		}
		return $return;
	}
}

/* ------- deprecated
class ResMenu extends TznDb {
	private static $instance;
	public function getInstance() {
		if($instance === null) {
			$instance = new ResMenu();
		}
		return $instance;
	}

	function __construct() {
		parent::TznDb('res_menu');
		$this->addProperties(array(
			'id' 		 => 'UID',
			'restaurant' => 'OBJ',
			'menu'  	 => 'OBJ'
		));
	}
}
--------*/


//------ menus ---------------------------//
class Categoria extends Generic {
	private static $instance;
	public function getInstance() {
		if($instance === null) {
			$instance = new Categoria();
		}
		return $instance;
	}

	function __construct() {
		parent::Generic('categoria');
		$this->addProperties(array(
			'img' 		 => 'STR', 
			'subtitulo'  => 'STR',
			'restaurant' => 'OBJ'  //-- added on version 1.1
		));
	}
	
	function gMenu() {
		if ($this ->id) {
			$tmp = Menu::getInstance();
			$tmp ->addWhere("categoria.categoriaId='{$this ->id}'");
			$tmp ->loadList();
			return $tmp ->rMore() ? $tmp : false;
		}
	}
	
	function gMenuCount() {
		$ret = 0;
		if ($tmp = $this ->gMenu())
			$ret = $tmp ->rCount();
		return $ret;
	}
}

class Menu extends Generic {
	private static $instance;
	public $_caracteristica = array();
	
	public function getInstance() {
		if($instance === null) {
			$instance = new Menu();
		}
		return $instance;
	}

	function __construct() {
		parent::Generic('menu');
		$this->addProperties(array(
			'categoria' => 'OBJ',
			'img' 		=> 'STR',
			'precio' 	=> 'DEC'
		));
	}
	
	function pSelectCheckBoxCaracteristica () {
		$items = Caracteristica::getInstance();
		if (!$items ->loadItems("#orderId ASC")) {
			print '<b>No existe a&uacute;n, caracteristicas, <a href="caracteristica-add.php">crear.</a></b>';
		} else {
			print '<select name="caracteristicaId" id="caracteristicaId" multiple="multiple" style="width:548px;"  >';
			print '	<option value=""></option>';
			$selecteds = $this ->gCaracteristica();
			while ($option = $items ->rNext()) {
				$yesNo = method_exists($selecteds, '_searchCaracteristica') ? ($selecteds ->_searchCaracteristica($option ->id) !== false ? "selected" : "" ) : "";
				print "<option value='{$option ->id}' $yesNo >{$option ->titulo}</option>";
			}
			print '</select>';
		}
	}
	
	function gCaracteristica() {
		if ($this ->id) {
			$tmp = MenuCaracteristica::getInstance();
			$tmp ->addWhere("menu.menuId='{$this ->id}' AND caracteristica.estado='". LBN_ADMIN_STATUS_ON ."'");
			$tmp ->loadList();
			return $tmp ->rMore() ? $tmp : false;
		}
	}
	
	
	private function _updateRelationship() {
		if ($this ->id) {
			$this ->query("DELETE FROM {$this ->gTable('menu_caracteristica')} WHERE menuId='{$this ->id}'");
			if (sizeof($this ->_caracteristica) > 0)
				foreach ($this ->_caracteristica as $item)
					$this ->query("INSERT INTO {$this ->gTable('menu_caracteristica')} (menuId, caracteristicaId) VALUES ('{$this ->id}', '$item')");
		}
	}
	
	function add() {
		if (parent::add ())
			$this ->_updateRelationship();
	}
	
	function update() {
		if (parent::update())
			$this ->_updateRelationship();
	}
	
	function pMenuConfigDetalle() {
		if ($mconfig = $this ->gMenuConfig(LBN_ADMIN_STATUS_ON)) {
			while ($item = $mconfig ->rNext()) 
				$item ->pDetails();
		}
	}
	
	function gMenuConfig($status=LBN_ADMIN_STATUS_ALL) {
		if ($this ->id) {
			$tmp = MenuConfig::getInstance();
			$tmp ->addOrder($tmp ->gTable().".orderId ASC");
			$tmp ->addWhere("menu.menuId='{$this ->id}'");
			if ($status != LBN_ADMIN_STATUS_ALL)  $tmp ->addWhere($tmp ->gTable().".estado='$status'");  
			$tmp ->loadList();
			return $tmp ->rMore() ? $tmp : false;
		}
	}
	
	function gMenuConfigCount() {
		$ret = 0;
		if ($tmp = $this ->gMenuConfig())
			$ret = $tmp ->rCount();
		return $ret;
	}
	
	
}


class MenuConfig extends Generic {
	private static $instance;
	public function getInstance() {
		if($instance === null) {
			$instance = new MenuConfig();
		}
		return $instance;
	}

	function __construct() {
		parent::Generic('menuconfig');
		$this->addProperties(array(
			'orderId' => 'NUM',				
			'type' => 'NUM',
			'menu' => 'OBJ'
		));
	}
	
	function gMenuConfigDetalle($status=LBN_ADMIN_STATUS_ALL) {
		if ($this ->id) {
			$tmp = MenuConfigDetalle::getInstance();
			$tmp ->addOrder($tmp ->gTable().".orderId ASC");
			$tmp ->addWhere("menuconfig.menuconfigId='{$this ->id}'");
			if ($status != LBN_ADMIN_STATUS_ALL)  $tmp ->addWhere($tmp ->gTable().".estado='$status'");
			$tmp ->loadList();
			return $tmp ->rMore() ? $tmp : false;
		}
	}
	
	function gMenuConfigDetalleCount() {
		$ret = 0;
		if ($tmp = $this ->gMenuConfigDetalle())
			$ret = $tmp ->rCount();
		return $ret;
	}
	
	function pDetails() {
		if ($details = $this ->gMenuConfigDetalle(LBN_ADMIN_STATUS_ON)) {
			print "<table><tr><td class='title' >{$this ->titulo}: </td><td>";     
			print $this ->type == 1 ? "<select name='item[]'>" : "<ul>";
			while ($item = $details ->rNext()) {
				print $this ->type == 1 ? "<option value='{$item ->id}'  >{$item ->titulo} ". ($item ->precio>0 ? " +(S/. ". number_format($item ->precio, 2) . ")" : "" ). "</option>" : "<li><label><input name='item[]' value='{$item ->id}' type='checkbox' />{$item ->titulo} ". ($item ->precio>0 ? " +(S/. ". number_format($item ->precio, 2) . ")" : "" )." </label></li>";
			}
			print $this ->type == 1 ? "</select>" : "</ul>";
			print "</td></tr></table>";
		}
	} 
}


class MenuConfigDetalle extends Generic {
	private static $instance;
	public function getInstance() {
		if($instance === null) {
			$instance = new MenuConfigDetalle();
		}
		return $instance;
	}

	function __construct() {
		parent::Generic('menuconfig_detalle');
		$this->addProperties(array(
				'orderId' => 'NUM',
				'precio' 	 => 'DEC',
				'menuconfig' => 'OBJ'
		));
	}
}


class Caracteristica extends Generic {
	private static $instance;
	public function getInstance() {
		if($instance === null) {
			$instance = new Caracteristica();
		}
		return $instance;
	}
	
	function __construct() {
		parent::Generic('caracteristica');
		$this->addProperties(array(
			'img' => 'STR'
		));
	}
	
}

class TipoCombo extends Generic {
	private static $instance;
	public function getInstance() {
		if($instance === null) {
			$instance = new TipoCombo();
		}
		return $instance;
	}

	function __construct() {
		parent::Generic('tipocombo');
	}
	
	function gCombos() {
		if ($this ->id) {
			$tmp = Combo::getInstance();
			$tmp ->addWhere("tipocombo.tipocomboId='{$this ->id}'");
			$tmp ->loadList();
			return $tmp ->rMore() ? $tmp : false;
		}
	}
	
	function gCombosCount() {
		$ret = 0;
		if ($tmp = $this ->gCombos())
			$ret = $tmp ->rCount();
		return $ret;
	}
}

class Combo extends Generic {
	private static $instance;
	public function getInstance() {
		if($instance === null) {
			$instance = new Combo();
		}
		return $instance;
	}

	function __construct() {
		parent::Generic('combo');
		$this->addProperties(array(
			'tipocombo' => 'OBJ'
		));
	}
}

class Adicional extends Generic {
	private static $instance;
	public function getInstance() {
		if($instance === null) {
			$instance = new Adicional();
		}
		return $instance;
	}

	function __construct() {
		parent::Generic('adicional');
	}
}


class Personalizado extends Generic {
	private static $instance;
	public function getInstance() {
		if($instance === null) {
			$instance = new Personalizado();
		}
		return $instance;
	}

	function __construct() {
		parent::Generic('personalizado');
	}
}


class MenuCaracteristica extends TznDb {
	private static $instance;
	public function getInstance() {
		if($instance === null) {
			$instance = new MenuCaracteristica();
		}
		return $instance;
	}

	function __construct() {
		parent::TznDb('menu_caracteristica');
		$this->addProperties(array(
				'id' 		 	 => 'UID',
				'menu' 			 => 'OBJ',
				'caracteristica' => 'OBJ'
		));
	}

	function _searchCaracteristica($id) {
		$return = false;
		$this->rReset();
		while($objItem = $this->rNext()) {
			if ($objItem->caracteristica ->id == $id ) {
				$return = $objItem;
				break;
			}
		}
		return $return;
	}
}

// ----- end menus --//


	
class File extends Generic {
	private static $myInstance = NULL;
	public static function getInstance() {
		if (! self::$myInstance) {
			self::$myInstance = new File ();
		}

		return self::$myInstance;
	}

	function __construct() {
		parent::Generic ( 'file' );
		$this->addProperties ( array ('type' => 'STR', 'target' => 'STR', 'size' => 'STR', 'restaurant' => 'OBJ', 'isProfile' => 'NUM' ) );
	}

	function delete() {
		if (parent::delete ()) {
			$this->removeFile ( 'target', false );
		}
	}

	function gImagePath() {
		if (!$this ->id) {
			$clone = &$this;
			$loaded = $clone ->rMore() ? $clone ->rNext() : null;
		} else {
			$loaded = clone $this; 
		}
		
		if ($loaded ->target) {
			return TZN_FILE_UPLOAD_PATH. $loaded ->target;
		}
		return TZN_FILE_IMAGES_PATH. 'no-image.jpg';
	}
}

class SysVariable extends TznDb {
	public static $cod_mctype = "MCT";
	
	private static $instance;
	public function getInstance() {
		if($instance === null) {
			$instance = new SysVariable();
		}
		return $instance;
	}

	function __construct() {
		parent::TznDb('sys_variable');
		$this->addProperties(array(
				'id' 		  => 'UID',
				'codigo' 	  => 'STR',
				'valor'		  => 'STR',
				'descripcion' => 'STR',
				'orderId' 	  => 'NUM',
				'status' 	  => 'STR',
		));
	}
	
	function pSelect($codFilter, $name, $default='', $nochoice='', $style='tznFormSelect', $xtra='') {
		$tmp = new SysVariable();
		$tmp ->addWhere("codigo='$codFilter'");
		$tmp ->addWhere("status='". LBN_ADMIN_STATUS_ON ."'");
		$tmp ->addOrder("orderId ASC");
		$tmp ->loadList();
		return $tmp ->qSelect($name, 'valor', $default, $nochoice, $style, $xtra);
	}
}

?>